<!-- 功能：新建组弹窗 | 作者：menghao.yan | 邮箱：yxs2615@163.com | 时间：2023年08月21日 22:59:06 -->
<template>
  <!-- 弹出框 -->
  <a-modal v-model:open="visible" title="新建组" width="500px" class="y-modal" okText="创建" @ok="onSubmit" @cancel="onCancel">
    <a-form :label-col="{ span: 6 }" :wrapper-col="{span: 14}">
      <a-form-item label="组名" v-bind="validateInfos.groupName">
        <a-input v-model:value="modelRef.groupName" placeholder="请输入组名" />
      </a-form-item>
      <a-form-item label="组ID" v-bind="validateInfos.groupId">
        <a-input v-model:value="modelRef.groupId" placeholder="请输入组ID" readonly />
      </a-form-item>
      <a-form-item label="组成员" v-bind="validateInfos.groupUsers">
        <a-row :gutter="12">
          <a-col flex="auto">
            <a-input v-model:value="modelRef.groupUsers" placeholder="请输入组成员" readonly />
          </a-col>
          <a-col flex="20px">
            <a-button type="link" class="y-btn" @click="openXuanZeYongHuModal">选择用户</a-button>
          </a-col>
        </a-row>
      </a-form-item>
    </a-form>
  </a-modal>

  <a-modal v-model:open="visibleXuanZeYongHuModal" title="选择用户" width="600px" class="y-modal" okText="确定" @ok="onSubmitXuanZeYongHu" @cancel="onCancelXuanZeYongHu">
    <a-card>
      <a-row align="middle" justify="end">
        <a-col :span="8">
          <a-input v-model:value="searchValueYongHu">
            <template #addonBefore>
              <search-outlined /> 搜索:
            </template>
          </a-input>
        </a-col>
      </a-row>
      <a-table bordered :data-source="dataSourceYongHu" :columns="columnsYongHu" class="y-table" :pagination="paginationYongHu"
               :row-selection="{ selectedRowKeys: state.selectedRowKeys, onChange: onSelectChange }"> </a-table>
    </a-card>
  </a-modal>
</template>

<script setup>
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  }
})

const { visible } = toRefs(props)

// 定义组件的事件
const emits = defineEmits(['update:visible'])

import { Form } from 'ant-design-vue'

const useForm = Form.useForm

const modelRef = reactive({
  groupName: '',
  groupId: '',
  groupUsers: ''
})

const rulesRef = reactive({
  groupName: [
    {
      required: true,
      message: '请输入组名'
    }
  ],
  groupId: [
    {
      required: true,
      message: '请输入组ID'
    }
  ],
  groupUsers: [
    {
      required: true,
      message: '请输入组成员'
    }
  ]
})

const { resetFields, validate, validateInfos, mergeValidateInfo } = useForm(modelRef, rulesRef)
const onSubmit = () => {
  validate()
    .then(() => {
      console.log('新建组>>', toRaw(modelRef))
      onCancel()
    })
    .catch(err => {
      console.log('error', err)
    })
}

const onCancel = () => {
  // 触发自定义事件来通知父组件关闭弹窗
  emits('update:visible', false)
}

// -- 选择用户
const visibleXuanZeYongHuModal = ref(false)

const openXuanZeYongHuModal = () => {
  visibleXuanZeYongHuModal.value = true
}

const searchValueYongHu = ref('')

// 表格列
const columnsYongHu = ref([
  {
    title: '用户名',
    dataIndex: 'userName',
    key: 'userName',
    align: 'center',
    sorter: {
      compare: (a, b) => a.userName.localeCompare(b.userName),
      multiple: 1
    }
  }
])

// 表格数据
const dataSourceYongHu = ref([
  {
    key: '1',
    name: 'John Brown',
    userId: '1',
    userName: 'john'
  },
  {
    key: '2',
    name: 'Jim Green',
    userId: '2',
    userName: 'jim'
  }
])

// 分页
const paginationYongHu = {
  current: 1,
  pageSize: 10,
  showQuickJumper: true,
  showSizeChanger: true,
  total: 0,
  showTotal: (total, range) => {
    return `${range[0]}-${range[1]} 共 ${total} 条数据`
  },
  onChange: (page, pageSize) => {
    console.log('用户管理 >  组选择用户分页改变>>', page, pageSize)
  }
}
const state = reactive({
  selectedRowKeys: []
})
const onSelectChange = selectedRowKeys => {
  console.log('用户管理 >  组选择用户多选改变', selectedRowKeys)
  state.selectedRowKeys = selectedRowKeys
}
const onSubmitXuanZeYongHu = () => {}

const onCancelXuanZeYongHu = () => {}
</script>

<style scoped>
.ant-row {
  margin-bottom: 10px;
}
.y-xinJianYongHu-title {
  color: #333;
  font-weight: bold;
  font-size: 16px;
}
</style>